﻿using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace CommonEngine.Tools
{
    /// <summary>
    /// GZIP String 扩展
    /// </summary>
    public static class GZipCompression
    {
        /// <summary>
        /// 将源字符串通过GZip压缩成byte[]数组(存储空间最小）
        /// 如果源字符串为空，仍然压缩
        /// 如果源数据为NULL，不压缩
        /// </summary>
        /// <param name="sourceString">源字符串</param>
        /// <returns>byte[]数组结果</returns>
        public static byte[] GzipToByteArray(this string sourceString)
        {
            if (sourceString == null)
            {
                return null;
            }
            byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(sourceString);
            byte[] compressAfterByte = Compress(compressBeforeByte);
            return compressAfterByte;
        }

        /// <summary>
        /// 将源字符串通过GZip压缩成Base64编码数组
        /// 如果源字符串为空，仍然压缩
        /// 如果源数据为NULL，不压缩
        /// </summary>
        /// <param name="sourceString">源字符串</param>
        /// <returns>压缩结果String</returns>
        public static string GzipToBase64String(this string sourceString)
        {
            if (sourceString == null)
            {
                return null;
            }
            string compressString = "";
            byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(sourceString);
            byte[] compressAfterByte = Compress(compressBeforeByte);
            compressString = Convert.ToBase64String(compressAfterByte);
            return compressString;
        }

        /// <summary>
        /// 将经过GZip压缩后生成的Base64字符串解压成源字符串
        /// </summary>
        /// <param name="CompressedBase64String">GZip压缩后后的Base64字符串</param>
        /// <returns>源字符串</returns>
        public static string UnGzipToString(this string CompressedBase64String)
        {
            if (string.IsNullOrEmpty(CompressedBase64String))
            {
                return string.Empty;
            }
            string decompressString = "";
            byte[] decompressBeforeByte = Convert.FromBase64String(CompressedBase64String);
            byte[] decompressAfterByte = Decompress(decompressBeforeByte);
            decompressString = Encoding.GetEncoding("UTF-8").GetString(decompressAfterByte);
            return decompressString;
        }

        /// <summary>
        /// 将经过GZip压缩后生成的byte[]数组解压成源字符串
        /// </summary>
        /// <param name="CompressedByteArray">GZip压缩后的byte[]数组</param>
        /// <returns>源字符串</returns>
        public static string UnGzipToString(this byte[] CompressedByteArray)
        {
            if (CompressedByteArray == null || CompressedByteArray.Length == 0)
            {
                return string.Empty;
            }
            string decompressString = "";
            byte[] decompressAfterByte = Decompress(CompressedByteArray);
            decompressString = Encoding.GetEncoding("UTF-8").GetString(decompressAfterByte);
            return decompressString;
        }

        private static byte[] Compress(byte[] data)
        {
            if (data == null || data.Length == 0)
                return data;
            try
            {
                MemoryStream ms = new MemoryStream();
                GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
                zip.Write(data, 0, data.Length);
                zip.Close();
                byte[] buffer = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, buffer.Length);
                ms.Close();
                return buffer;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private static byte[] Decompress(byte[] data)
        {
            if (data == null || data.Length == 0)
                return data;
            try
            {
                MemoryStream ms = new MemoryStream(data);
                GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
                MemoryStream msreader = new MemoryStream();
                byte[] buffer = new byte[0x1000];
                while (true)
                {
                    int reader = zip.Read(buffer, 0, buffer.Length);
                    if (reader <= 0)
                    {
                        break;
                    }
                    msreader.Write(buffer, 0, reader);
                }
                zip.Close();
                ms.Close();
                msreader.Position = 0;
                buffer = msreader.ToArray();
                msreader.Close();
                return buffer;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
